Skip to content

feat!: per-event capture options and legacy property hoisting - #1023

Open
eli-r-ph wants to merge 1 commit into
v1-capture-ai-configfrom
v1-capture-options
Open

eli-r-ph wants to merge 1 commit into
v1-capture-ai-configfrom
v1-capture-options

Conversation

@eli-r-ph

@eli-r-ph eli-r-ph commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

Capture v1 sends processing options (process_person_profile, cookieless_mode, disable_skew_correction, product_tour_id) in a per-event options object. Until now the SDK only filled it from legacy $ properties, coerced the values, and gave callers no way to set options directly. posthog-go and posthog-rs v1 already take per-event options and send them as given.

New options argument on capture, capture_ai, set, set_once, group_identify, alias and capture_exception, on Client, AsyncPosthog and the module-level functions. For example: posthog.capture("signed_up", distinct_id=..., options={"cookieless_mode": True}).

Behavior changes:

  • Options are sent as given. The SDK checks neither keys nor value types; PostHog validates them. Unknown keys pass through. A value that is not a dict is logged as an error, and the event is sent without it.
  • Legacy properties are no longer coerced. $cookieless_mode, $ignore_sent_at, $product_tour_id and $process_person_profile still fill the matching option, unchanged. Before, "true" became True and an unparseable value was dropped. The property is always removed from properties.
  • An option wins over its legacy property. A null option counts as unset, so the property fills it. This matches go and rs.
  • The legacy properties move into options once, after before_send. That was already the case. before_send now also sees options and can change it.
  • Event uuids accept only the forms other SDKs accept:
    • hyphenated, 32 hex, {...} braced and urn:uuid: in any case; each is canonicalized
    • anything else, such as Python's uuid: prefix or misplaced hyphens, is replaced with a generated uuid and logged
    • this now applies to AsyncPosthog too, which before accepted anything uuid.UUID() parses
  • Minimal $feature_flag_called events keep $cookieless_mode, $ignore_sent_at, $product_tour_id and $window_id. The allowlist runs before the legacy properties move into options, so these were dropped and their options lost. posthog-go has the same fix in feat!: add default and request-context options; event values win over defaults posthog-go#359.

Not in this PR: global and context options, the layer-order change, and the personless option (next PR in the stack), and AI wrappers and MCP moving to options (the one after).

💚 How did you test it?

  • Wire builder:
    • each legacy property fills its option unchanged
    • option vs legacy, with the option set, null or missing
    • unknown options pass through
    • a non-dict is logged and ignored
    • the input is not mutated
    • accepted and rejected uuid forms
  • Clients: new test_event_options.py. It checks that options reaches the wire from every method on Client and AsyncPosthog. A before_send that returns a new event can change options and remove a legacy property. A non-dict options is logged and the event is still sent.
  • Minimal flag events: a gated event with legacy option super properties keeps the options and window_id.
  • Break-on-purpose: each of these was reverted in turn, and the matching test failed every time:
    • the legacy property winning
    • alias dropping options
    • the four allowlist keys
    • lenient uuid parsing
    • async capture dropping options
    • sync and async ignoring the hook's options
  • I ran ruff, mypy (baseline), the strict type smoke, the full pytest suite, the adapter tests, python -W error -c "import posthog", the public API snapshot and uv lock --check.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

Covered by the existing capture-v1-major changeset; the migration guide lands later in this stack.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Written with Cursor (Claude Opus) under the direction of the assignee.

Agreed before implementation:

  • options pass through unchanged
  • non-dict options logged and ignored
  • only string $session_id/$window_id move to top-level fields
  • hoisting once, after before_send
  • an option wins over its legacy property
  • the uuid forms, matching posthog-go uuid.Validate

Agent calls worth review:

  • the queued message always carries options (maybe {}), so before_send sees the key
  • a null legacy property still fills an unset option as null, as in posthog-rs

@eli-r-ph eli-r-ph self-assigned this Oct 7, 2026
@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-10-08T16:31:12.602809+00:00
Duration: 247697ms

✅ All Tests Passed!

116/116 tests passed


Capture_V1 Tests

✅ 95/95 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint ✅ 522ms
Endpoint And Method.Does Not Use Legacy Endpoints ✅ 513ms
Required Headers.Has Authorization Bearer Header ✅ 514ms
Required Headers.Has Content Type Json ✅ 515ms
Required Headers.Has Posthog Sdk Info Format ✅ 512ms
Required Headers.Has Posthog Attempt Header ✅ 512ms
Required Headers.Has Posthog Request Id ✅ 515ms
Required Headers.Has Posthog Request Timestamp ✅ 513ms
Required Headers.Has User Agent ✅ 513ms
Body Format.Body Has Created At And Batch ✅ 514ms
Body Format.No Api Key In Body ✅ 514ms
Body Format.No Sent At In Body ✅ 514ms
Event Format.Event Has Required Root Fields ✅ 514ms
Event Format.Event Uuid Is Valid ✅ 514ms
Event Format.Event Timestamp Is Rfc3339 ✅ 513ms
Event Format.Non Utc Event Timestamp Is Converted To Utc ✅ 520ms
Event Format.Distinct Id Is String ✅ 514ms
Event Format.Distinct Id At Root Not Properties ✅ 514ms
Event Format.Custom Properties Preserved ✅ 514ms
Event Format.Set Properties Preserved ✅ 514ms
Event Format.Set Once Properties Preserved ✅ 515ms
Event Format.Groups Properties Preserved ✅ 515ms
Event Format.Sdk Generates Uuid If Not Provided ✅ 515ms
Event Format.Event Has Required Root Fields Batch ✅ 519ms
Event Format.Event Uuid Is Valid Batch ✅ 518ms
Event Format.Event Timestamp Is Rfc3339 Batch ✅ 520ms
Event Format.Distinct Id Is String Batch ✅ 517ms
Event Format.Distinct Id At Root Not Properties Batch ✅ 516ms
Event Format.Custom Properties Preserved Batch ✅ 519ms
Event Format.Set Properties Preserved Batch ✅ 518ms
Event Format.Set Once Properties Preserved Batch ✅ 518ms
Event Format.Groups Properties Preserved Batch ✅ 517ms
Event Format.Sdk Generates Uuid If Not Provided Batch ✅ 516ms
Batch Behavior.Multiple Events In Single Batch ✅ 519ms
Batch Behavior.Batch Envelope Smoke ✅ 518ms
Batch Behavior.Flush With No Events Sends Nothing ✅ 509ms
Batch Behavior.Flush At Triggers Batch ✅ 1015ms
Batch Behavior.Created At Reflects Batch Creation Time ✅ 516ms
Deduplication.Generates Unique Uuids ✅ 520ms
Deduplication.Different Events Same Content Different Uuids ✅ 515ms
Deduplication.Preserves Uuid On Retry ✅ 6523ms
Deduplication.Preserves Timestamp On Retry ✅ 6525ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 6523ms
Deduplication.No Duplicate Events In Batch ✅ 520ms
Header Behavior On Retry.Attempt Header Starts At One ✅ 512ms
Header Behavior On Retry.Attempt Header Increments On Retry ✅ 12532ms
Header Behavior On Retry.Request Id Preserved On Retry ✅ 6523ms
Header Behavior On Retry.Different Requests Have Different Request Ids ✅ 3026ms
Header Behavior On Retry.Request Timestamp Changes On Retry ✅ 6522ms
Response Format Validation.Success Response Has Uuid Keyed Results ✅ 514ms
Response Format Validation.Success Response Has Ok For Each Event ✅ 518ms
Response Format Validation.Success No Retry After When All Ok ✅ 517ms
Response Format Validation.Success Retry After Present When Retry Events ✅ 1520ms
Response Format Validation.Success No Retry After When Drop Only ✅ 516ms
Response Format Validation.Response Echoes Request Id ✅ 513ms
Retry Behavior.Retries On 408 ✅ 6520ms
Retry Behavior.Retries On 500 ✅ 6522ms
Retry Behavior.Retries On 503 ✅ 7526ms
Retry Behavior.Retries On 504 ✅ 6526ms
Retry Behavior.Retryable Errors Have Retry After ✅ 3521ms
Retry Behavior.Respects Retry After On Retryable Error ✅ 11526ms
Retry Behavior.Does Not Retry On 400 ✅ 2517ms
Retry Behavior.Does Not Retry On 401 ✅ 2520ms
Retry Behavior.Does Not Retry On 402 ✅ 2517ms
Retry Behavior.Does Not Retry On 413 ✅ 2518ms
Retry Behavior.Does Not Retry On 415 ✅ 2517ms
Retry Behavior.Non Retryable Errors Have No Retry After ✅ 2516ms
Retry Behavior.Implements Backoff ✅ 18543ms
Retry Behavior.Max Retries Respected ✅ 18543ms
Partial Batch Handling.Handles 200 Full Success ✅ 2518ms
Partial Batch Handling.Handles 200 With All Ok ✅ 3522ms
Partial Batch Handling.Does Not Retry Dropped Events ✅ 3519ms
Partial Batch Handling.Does Not Retry Limited Events ✅ 3522ms
Partial Batch Handling.Prunes Ok Events On Partial Retry ✅ 6529ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry ✅ 6525ms
Partial Batch Handling.Retries Only Retry Events From Partial ✅ 6528ms
Partial Batch Handling.Partial Retry Preserves Uuids ✅ 6521ms
Partial Batch Handling.Partial Retry Attempt Header Increments ✅ 6524ms
Partial Batch Handling.Partial Retry Request Id Preserved ✅ 6524ms
Partial Batch Handling.Respects Retry After On Partial ✅ 8522ms
Partial Batch Handling.Unknown Result Treated As Terminal ✅ 3520ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry ✅ 3521ms
Compression.Sends Gzip Content Encoding ✅ 515ms
Compression.No Content Encoding When Disabled ✅ 515ms
Compression.Compressed Body Is Decompressible ✅ 513ms
Error Handling.Does Not Retry On Unknown 4Xx ✅ 2515ms
Event Options.Cookieless Mode Override ✅ 513ms
Event Options.Disable Skew Correction Override ✅ 512ms
Event Options.Process Person Profile Override ✅ 514ms
Event Options.Product Tour Id Override ✅ 513ms
Event Options.Unset Options Omitted ✅ 512ms
Event Options.Options Override In Batch ✅ 515ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties ✅ 512ms
Geoip And Historical Migration.Historical Migration Set In Body ✅ 513ms
Geoip And Historical Migration.Historical Migration Absent By Default ✅ 512ms

Feature_Flags Tests

✅ 17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id ✅ 15ms
Request Payload.Flags Request Uses V2 Query Param ✅ 12ms
Request Payload.Flags Request Hits Flags Path Not Decide ✅ 12ms
Request Payload.Flags Request Omits Authorization Header ✅ 12ms
Request Payload.Token In Flags Body Matches Init ✅ 12ms
Request Payload.Groups Round Trip ✅ 12ms
Request Payload.Groups Default To Empty Object ✅ 12ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False ✅ 12ms
Request Payload.Disable Geoip Omitted Defaults To False ✅ 12ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key ✅ 11ms
Request Lifecycle.No Flags Request On Init Alone ✅ 4ms
Request Lifecycle.No Flags Request On Normal Capture ✅ 510ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests ✅ 18ms
Request Lifecycle.Mock Response Value Is Returned To Caller ✅ 11ms
Retry Behavior.Retries Flags On 502 ✅ 316ms
Retry Behavior.Retries Flags On 504 ✅ 316ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event ✅ 512ms

Feature_Flags_Local_Evaluation Tests

✅ 4/4 tests passed

View Details
Test Status Duration
Versioned Boolean Matching.Matching Version Missing ✅ 70ms
Versioned Boolean Matching.Matching Version 1 ✅ 65ms
Versioned Boolean Matching.Matching Version 2 ✅ 65ms
Versioned Boolean Matching.Version Only Reload 1 2 1 2 Missing ✅ 40ms

@eli-r-ph eli-r-ph mentioned this pull request Oct 7, 2026
3 of 20 tasks
@eli-r-ph
eli-r-ph force-pushed the v1-capture-options branch from 891b4c8 to a515200 Compare October 7, 2026 02:03
@eli-r-ph
eli-r-ph force-pushed the v1-capture-ai-config branch from 0ae6eb7 to 8da410d Compare October 7, 2026 02:03
@eli-r-ph
eli-r-ph force-pushed the v1-capture-options branch from a515200 to ea1d279 Compare October 7, 2026 17:43
@eli-r-ph

eli-r-ph commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

@greptileai review

@eli-r-ph
eli-r-ph marked this pull request as ready for review October 7, 2026 20:43
@eli-r-ph
eli-r-ph requested a review from a team as a code owner October 7, 2026 20:43
Comment thread posthog/async_client.py
uuid: Optional[Union[str, UUID]] = None,
disable_geoip: Optional[bool] = None,
distinct_id: Optional[ID_TYPES] = None,
options: Optional[Dict[str, Any]] = None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth using stronger types? e.g.

 class CaptureOptions(TypedDict, total=False):
     cookieless_mode: Optional[bool]
     disable_skew_correction: Optional[bool]
     process_person_profile: Optional[bool]

@eli-r-ph eli-r-ph Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 good callout I considered this - as long as no SDK code changes are needed for future options once we define them, the solution does what we want 👍

I rejected TypedDict here so far for two reasons:

  1. the backend does a curated lenient parse on these values for "boolean-intent" that will shore up some current silent fails in legacy capture, that will now apply uniformly across v1 SDKs; with TypedDict, legacy hoisted props would fail hard on non-bool values just on this SDK, or we'd need to dup the backend parsing then keep it in parity w/capture
  2. I think as we add future options to TypedDict that aren't in the schema, mypy and friends might flag end user call sites that apply them with warnings?

If I'm missing something let me know, happy to adjust, but I'm leaning towards keeping this as is for those reasons?

@dustinbyrne
dustinbyrne requested a review from a team October 7, 2026 23:02
@eli-r-ph
eli-r-ph force-pushed the v1-capture-options branch from ea1d279 to 9705c81 Compare October 8, 2026 16:25
@eli-r-ph
eli-r-ph force-pushed the v1-capture-ai-config branch from ba59e9d to 44d141d Compare October 8, 2026 16:25

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants